import 'package:flutter/material.dart';

class HomeCourse extends StatefulWidget {
  List courseList;
  HomeCourse({Key key, @required this.courseList}) : super(key: key);

  @override
  _HomeCourseState createState() => _HomeCourseState();
}

class _HomeCourseState extends State<HomeCourse> {
  @override
  Widget build(BuildContext context) {
    return SliverList(
      delegate: SliverChildBuilderDelegate(
        (BuildContext context, int index) {
          var course = widget.courseList[index];
          var teacherName = course['teacherDTO'] == null ? '' : course['teacherDTO']['teacherName'];
          var description = course['teacherDTO'] == null ? '' : course['teacherDTO']['description'];
          return GestureDetector(
            onTap: () {},
            child: Container(
              color: Colors.white,
              padding: EdgeInsets.all(10),
              child: Flex(
                direction: Axis.horizontal,
                children: [
                  Expanded(
                    child: ClipRRect(
                      borderRadius: BorderRadius.circular(10),
                      child: Image.network(
                        course['courseListImg'],
                        fit: BoxFit.cover,
                        height: 120
                      ),
                    ),
                    flex: 1
                  ),
                  Expanded(
                    child: Container(
                      constraints: BoxConstraints(
                        minHeight: 120,
                      ),
                      padding: EdgeInsets.symmetric(horizontal: 20),
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Container(
                            width: double.infinity,
                            child: Text(
                              course['courseName'],
                              style: TextStyle(
                                fontSize: 16,
                              ),
                              overflow: TextOverflow.ellipsis,
                            ),
                          ),
                          Container(
                            width: double.infinity,
                            child: Text(
                              course['brief'],
                              style: TextStyle(
                                fontSize: 14,
                              ),
                              overflow: TextOverflow.ellipsis
                            ),
                          ),
                          Wrap(
                            children: [
                              Container(
                                padding: EdgeInsets.all(5),
                                color: Colors.grey[300],
                                height: 28,
                                child: Text(
                                  teacherName,
                                  style: TextStyle(fontSize: 12, height: 1.5)
                                ),
                              ),
                              SizedBox(width: 10),
                              Container(
                                padding: EdgeInsets.all(5),
                                color: Colors.grey[300],
                                height: 28,
                                child: Text(
                                  description,
                                  style: TextStyle(fontSize: 12, height: 1.5)
                                ),
                              )
                            ]
                          ),
                          Row(
                            children: [
                              Container(
                                padding: EdgeInsets.all(5),
                                child: Text(
                                  '￥' + course['discounts'].toString(),
                                  style: TextStyle(
                                    fontSize: 18,
                                    fontWeight: FontWeight.w500,
                                    color: Colors.red
                                  )
                                ),
                              ),
                              SizedBox(width: 10),
                              Container(
                                padding: EdgeInsets.all(5),
                                child: Text(
                                  course['sales'].toString() + '人购买',
                                  style: TextStyle(fontSize: 14)
                                ),
                              )
                            ]
                          ),
                        ],
                      ),
                    ),
                    flex: 3
                  )
                ],
              )
            )
          );
        },
        childCount: widget.courseList.length,
      ),
    );
  }
}